<?php

namespace App\Models\Applet\Shop;

class ShopGoodsModel extends BaseShopModel
{
    protected $table = 'shop_goods';

    const STATUS_NO  = 0; // 未上架
    const STATUS_YES = 1; // 已上架

    /**
     * 总条数
     * @param array $params
     * @return int
     */
    public function total(array $params): int
    {
        return $this->where('status', self::STATUS_YES)
                    ->where('status', self::STATUS_YES)
                    ->where('deleted', self::DELETED_NO)
                    ->count();
    }

    /**
     * 总条数
     * @param array $params
     * @return int
     */
    public function adminTotal(array $params): int
    {
        $query = $this;
        if (!empty($params['keyword'])) {
            $whereRaw = '(name like "%' . $params['keyword'] . '%"' .
                        ' or status like "%' . $params['keyword'] . '%")';
            if (is_numeric($params['keyword'])) {
                $whereRaw = trim($whereRaw, ')');
                $whereRaw .= ' or cast(id as char) like "%' . $params['keyword'] . '%")';
            }
            $query = $query->whereRaw($whereRaw);
        }
        if (isset($params['status']) && $params['status'] != -1) {
            $query = $query->where('status', $params['status']);
        }

        return $query->where('deleted', self::DELETED_NO)
                     ->count();
    }

    /**
     * 列表
     * @param array $params
     * @param array $fields
     * @return array
     */
    public function list(array $params, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $query = $this->select($fields)
                      ->where('status', self::STATUS_YES)
                      ->where('deleted', self::DELETED_NO);
        // 分类
        if (!empty($params['categoryId'])) {
            $query = $query->where('category_id', $params['categoryId']);
        }

        if (isset($params['minPrice']) && isset($params['maxPrice'])) {
            $minPrice = intval($params['minPrice'] * 100);
            $maxPrice = intval($params['maxPrice'] * 100);
            $query = $query->whereBetween('price', [$minPrice, $maxPrice]);
        }

        // 排序
        if (!empty($params['priceSort'])) {
            $query = $query->orderBy('price', $params['priceSort']);
        } else {
            $query = $query->orderBy('is_recommend', 'desc')
                           ->orderBy('listing_time', 'desc');
        }

        return $query->orderBy('id', 'desc')
                     ->offset((CURRENT_PAGE - 1) * PAGE_SIZE)
                     ->limit(PAGE_SIZE)
                     ->get()
                     ->toArray();
    }

    /**
     * 列表
     * @param array $params
     * @param array $fields
     * @return array
     */
    public function adminList(array $params, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $query = $this;
        if (!empty($params['keyword'])) {
            $whereRaw = '(name like "%' . $params['keyword'] . '%"' .
                        ' or status like "%' . $params['keyword'] . '%")';
            if (is_numeric($params['keyword'])) {
                $whereRaw = trim($whereRaw, ')');
                $whereRaw .= ' or cast(id as char) like "%' . $params['keyword'] . '%")';
            }
            $query = $query->whereRaw($whereRaw);
        }
        if (isset($params['status']) && $params['status'] != -1) {
            $query = $query->where('status', $params['status']);
        }

        return $query->where('deleted', self::DELETED_NO)
                     ->orderBy('id', 'desc')
                     ->offset((CURRENT_PAGE - 1) * PAGE_SIZE)
                     ->limit(PAGE_SIZE)
                     ->get()
                     ->toArray();
    }

    /**
     * 根据分类id查询数据
     * @param int $categoryId
     * @param array $fields
     * @return array
     */
    public function getItemByCategoryId(int $categoryId, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        $result = $this->select($fields)
                       ->where('category_id', $categoryId)
                       ->where('deleted', self::DELETED_NO)
                       ->first();
        return $result ? $result->toArray() : [];
    }

    /**
     * 根据id获取列表
     * @return void
     */
    public function getItemsByIdAndDeletedAll(array $ids, array $fields = self::QUERY_DEFAULT_FIELDS): array
    {
        return $this->select($fields)
                    ->whereIn('id', $ids)
                    ->get()
                    ->toArray();
    }
}
